/**
 * 
 */
package org.xiha.wiki.database;

import java.io.File;

import org.apache.commons.lang.StringUtils;

import com.sleepycat.bind.serial.StoredClassCatalog;
import com.sleepycat.je.Cursor;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;

/**
 * @author summer
 * 
 */
public class BasicDatabase {
    private Environment         env;
    
    private static final String CLASS_CATALOG     = "java_class_catalog";
    private static final String ACK_STORE         = "ACK_STORE";
    private static final String ACK_CATALOG_STORE = "ACK_CATALOG_STORE";
    
    private static final String DB_HOME           = "WIKI_DB_HOME";
    private Database            ackDb;
    private Database            ackCatalogDb;
    private Database            catalogDb;
    private DatabaseConfig      dbConfig;
    
    private StoredClassCatalog  javaCatalog;
    private Cursor              ackCursor;
    
    public BasicDatabase(String dbHome) {
        if (StringUtils.indexOf(dbHome, "~") == 0) {
            dbHome = System.getenv().get(DB_HOME) + StringUtils.substring(dbHome, 1);
        }
        
        EnvironmentConfig envConfig = new EnvironmentConfig();
        envConfig.setTransactional(true);
        envConfig.setAllowCreate(true);
        env = new Environment(new File(dbHome), envConfig);
        
        dbConfig = new DatabaseConfig();
        dbConfig.setTransactional(true);
        dbConfig.setAllowCreate(true);
        
    }
    
    private void openForAll() {
        catalogDb = env.openDatabase(null, CLASS_CATALOG, dbConfig);
        javaCatalog = new StoredClassCatalog(catalogDb);
        
    }
    
    public void openAckWithCursor() {
        openForAll();
        ackDb = env.openDatabase(null, ACK_STORE, dbConfig);
        ackCursor = ackDb.openCursor(null, null);
        
    }
    
    public void closeAckWithCursor() {
        if (ackCursor != null)
            ackCursor.close();
        if (ackDb != null)
            ackDb.close();
        closeForAll();
    }
    
    public void openAckCatalog() {
        openForAll();
        ackCatalogDb = env.openDatabase(null, ACK_CATALOG_STORE, dbConfig);
        
    }
    
    public void closeAckCatalog() {
        if (ackCatalogDb != null)
            ackCatalogDb.close();
        closeForAll();
    }
    
    public void openAck() {
        openForAll();
        ackDb = env.openDatabase(null, ACK_STORE, dbConfig);
    }
    
    public void closeAck() {
        if (ackDb != null)
            ackDb.close();
        closeForAll();
    }
    
    private void closeForAll() {
        if (javaCatalog != null)
            javaCatalog.close();
        if (catalogDb != null)
            catalogDb.close();
        if (env != null)
            env.close();
    }
    
    public final Cursor getAckCursor() {
        return ackCursor;
    }
    
    public final Database getAckCatalogDb() {
        return ackCatalogDb;
    }
    
    public final Database getAckDb() {
        return ackDb;
    }
    
    public final StoredClassCatalog getJavaCatalog() {
        return javaCatalog;
    }
    
    public final Environment getEnvironment() {
        return env;
    }
}
